﻿Imports System.ComponentModel
Imports System.Reflection
Imports System.Resources
Imports System.Drawing
Imports System.Windows.Forms

''' <summary>
''' 任务的基础类
''' Author : ajayumi
''' Date   : 2010-10-14
''' </summary>
Public MustInherit Class ActionBase
    Implements IDisposable


#Region "variable"
    Private m_Timer As Timer = Nothing

    Protected m_ImageInfoList As List(Of ImageInfo) = Nothing
    Protected m_Index As Integer = 0
    Protected Const TIMER_INTERVAL As Integer = 200

    Protected Shared m_Assembly As Assembly = Nothing
    Protected m_ResourceManager As ResourceManager = Nothing

#End Region

#Region "properties"

    Private m_Name As String = String.Empty
    ''' <summary>
    ''' 名称
    ''' </summary>
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property


    Private m_LowIndex As Integer = 0
    ''' <summary>
    ''' 下标
    ''' </summary>
    Public Property LowIndex() As Integer
        Get
            Return m_LowIndex
        End Get
        Set(ByVal value As Integer)
            m_LowIndex = value
        End Set
    End Property


    Private m_UpperIndex As Integer = 0
    ''' <summary>
    ''' 上标
    ''' </summary>
    Public Property UpperIndex() As Integer
        Get
            Return m_UpperIndex
        End Get
        Set(ByVal value As Integer)
            m_UpperIndex = value
        End Set
    End Property


    Private m_Total As Integer = 0
    ''' <summary>
    ''' 总图片数量
    ''' </summary>
    Public Property Total() As Integer
        Get
            Return m_Total
        End Get
        Set(ByVal value As Integer)
            m_Total = value
        End Set
    End Property


    Private m_Duration As Integer = 1000
    ''' <summary>
    ''' 动作的持续时间，单位是毫秒
    ''' </summary>
    Public Property Duration() As Integer
        Get
            Return m_Duration
        End Get
        Set(ByVal value As Integer)
            m_Duration = value
        End Set
    End Property

    Private m_IsBusy As Boolean = False
    ''' <summary>
    ''' 当前动作是否忙碌
    ''' </summary>
    Public Property IsBusy() As Boolean
        Get
            Return m_IsBusy
        End Get
        Set(ByVal value As Boolean)
            m_IsBusy = value

            If value = True Then
                Me.m_Timer.Enabled = True
            Else
                Me.m_Timer.Enabled = False
                Me.m_Index = 0
            End If
        End Set
    End Property


#End Region

#Region "events"

    Public Event ResourcesProgressChanged As ProgressChangedEventHandler
    Protected Overridable Sub OnResourcesProgressChanged(ByVal e As ProgressChangedEventArgs)
        RaiseEvent ResourcesProgressChanged(Me, e)
    End Sub

    Public Event ResourcesCompleted As AsyncCompletedEventHandler
    Protected Overridable Sub OnResourcesCompleted(ByVal e As AsyncCompletedEventArgs)
        RaiseEvent ResourcesCompleted(Me, e)
    End Sub

    Public Event ImageChanged As Action(Of Object, ImageChangedEventArgs)
    Protected Overridable Sub OnImageChanged(ByVal e As ImageChangedEventArgs)
        RaiseEvent ImageChanged(Me, e)
    End Sub

#End Region

#Region "methods"

    '[protected]---------------------------------------------------------------------
    Protected Overridable Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        If Me.m_ImageInfoList Is Nothing OrElse Me.m_ImageInfoList.Count = 0 Then
            Me.Dispose()
            System.Environment.Exit(System.Environment.ExitCode)
        End If

        Me.m_Index += 1
        If Me.m_Index >= Me.m_ImageInfoList.Count Then
            Me.ActionCompleted()
        End If

        Dim _ImageInfo As ImageInfo = Me.m_ImageInfoList(Me.m_Index)
        Dim _Image As Image = Me.GetImageResource(_ImageInfo.Name)

        Me.OnImageChanged(New ImageChangedEventArgs(_ImageInfo, _Image))
    End Sub

    ''' <summary>
    ''' 动作完成
    ''' </summary>
    Protected Overridable Sub ActionCompleted()
        Me.m_Index = 0
        Console.WriteLine("ActionCompleted 被 {0} 调用", Me.ToString())
    End Sub

    ''' <summary>
    ''' 添加ImageInfo对象到列表中
    ''' </summary>
    ''' <param name="imgInfo">ImageInfo对象</param>
    Protected Overridable Sub [Add](ByVal imgInfo As ImageInfo)
        Me.m_ImageInfoList.Add(imgInfo)
    End Sub

    ''' <summary>
    ''' 从ResourceManager中获取图片资源
    ''' </summary>
    ''' <param name="name">图片资源名称</param>
    ''' <returns>Image</returns>
    Protected Function GetImageResource(ByVal name As String) As Image
        Return DirectCast(Me.m_ResourceManager.GetObject(name), Image)
    End Function


    '[public]---------------------------------------------------------------------
    ''' <summary>
    ''' 初始化ImageInfo列表
    ''' </summary>
    Public MustOverride Sub Init()

    ''' <summary>
    ''' 初始化ImageInfo列表
    ''' </summary>
    ''' <param name="state">可以是任何对象，这里我们需要的是BackgroundWorker控件</param>
    Public MustOverride Sub Init(ByVal state As Object)

    ''' <summary>
    ''' 开始动画
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub [Play]()
        Console.WriteLine("开始 {0} ...", Me.ToString())

        Me.IsBusy = True
    End Sub

    ''' <summary>
    ''' 停止动画
    ''' </summary>
    Public Overridable Sub [Stop]()
        Console.WriteLine("停止 {0}", Me.ToString())

        Me.IsBusy = False
    End Sub

    ''' <summary>
    ''' 获取第一个Image对象，如果列表为空，则创建一个空白的Image对象
    ''' </summary>
    ''' <returns>Image对象</returns>
    Public Overridable Function First() As Image
        Dim _Image As Image = Nothing

        If Me.m_ImageInfoList Is Nothing OrElse Me.m_ImageInfoList.Count = 0 Then
            _Image = New Bitmap(10, 10)
        Else
            _Image = Me.GetImageResource(Me.m_ImageInfoList.First().Name)
        End If

        Return _Image
    End Function


#End Region

    Public Sub New()
        Me.m_ImageInfoList = New List(Of ImageInfo)()
        Me.m_Timer = New Timer() With {.Interval = TIMER_INTERVAL}
        AddHandler Me.m_Timer.Tick, AddressOf Me.Timer_Tick
    End Sub

    Public Sub New(ByVal assemblyName As String)
        Me.m_ImageInfoList = New List(Of ImageInfo)()
        m_Assembly = Reflection.Assembly.Load(assemblyName)

        Me.m_Timer = New Timer() With {.Interval = TIMER_INTERVAL}
        AddHandler Me.m_Timer.Tick, AddressOf Me.Timer_Tick
    End Sub





    Private disposedValue As Boolean = False        ' 检测冗余的调用

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 释放其他状态(托管对象)。
            End If

            ' TODO: 释放您自己的状态(非托管对象)。
            ' TODO: 将大型字段设置为 null。
            If Me.m_ImageInfoList IsNot Nothing Then
                Me.m_ImageInfoList.Clear()
                Me.m_ImageInfoList = Nothing
            End If

            If Me.m_Timer IsNot Nothing Then
                Me.m_Timer.Dispose()
                Me.m_Timer = Nothing
            End If
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' Visual Basic 添加此代码是为了正确实现可处置模式。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
